package com.cloud.Bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MyBeanPostProcessor implements BeanPostProcessor {
    //其中postProcessBeforeInitialization方法会在每一个bean对象的初始化方法调用之前回调；

    /**
     * 在调用init初始化之前 可以修改原有bean对象 如果返回是为null 则直接返回原有bean对象
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("mayiktBean".equals(beanName)) {
            log.info("MayiktBean before--实例化的bean对象:" + bean + "t" + beanName);
            return bean;
        }
        return bean;
    }


    //postProcessAfterInitialization方法会在每个bean对象的初始化方法调用之后被回调。
    //可以修改原有bean对象 如果返回是为null 则直接返回原有bean对象
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("mayiktBean".equals(beanName))
            log.info("MayiktBean after...实例化的bean对象:" + bean + "t" + beanName);
        return bean;
    }
}